package news.yp.newsproject.presenter.presenter;


import android.annotation.SuppressLint;
import android.util.Log;

import com.news.yp.baselibrary.base.BasePresenter;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;
import news.yp.newsproject.bean.BookRequest;
import news.yp.newsproject.bean.BookResponse;
import news.yp.newsproject.presenter.model.DiscoverModel;

/**
 * 发现的p层
 */
public class DiscorPresenter extends BasePresenter<CenterPresenter.ViewPresenter> {

    private final DiscoverModel model;

    public DiscorPresenter() {
        model = new DiscoverModel();
    }

    @SuppressLint("CheckResult")
    public void getPageData(int page, int num) {
        getView().onLoading();
        BookRequest request = new BookRequest();
        request.setBooks("books");
        request.setNum(num);
        request.setPage(page);
        model.getData(request)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<BookResponse>() {
                    @Override
                    public void accept(BookResponse bookResponse) throws Exception {
                        if (bookResponse != null) {
                            getView().onSuccess(bookResponse);
                        } else {
                            throw new Exception("数据为为空");
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        Log.e("TAG", "请求书籍信息发生异常" + throwable.getMessage().toString());
                        getView().onError();
                    }
                });
        //     getView().onSuccess();
    }
}
